// pages/zhenggai/zhenggai.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [0,1,2,3,4,5,6,7,8,9],
    index:1,
    checkImages: [],
    rectItem:"",
    uploadflag:false
  },
  bindPickerChange: function (e) {
    this.setData({
    index: e.detail.value
    })
  },
  initImages: function (num) {
    var result = [];
    for (var i = 0; i < num; i++) {
      var img = {
        filePath: "",
        hasImage: false,
        index: i,
        imageUrl:""
      }
      result.push(img);
    }
    this.setData({ checkImages: result });
  },
  chooseImageUi: function (event) {
    var that = this;
    var num = event.currentTarget.dataset.index;
    var currentImage = this.data.checkImages[num];
    wx.chooseImage({
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        
        that.uploadImage(res.tempFilePaths[0], function (data) {
          currentImage.filePath = data.url;
          currentImage.hasImage = true;
          currentImage.imageUrl = data.url;
          that.data.checkImages[num] = currentImage;
          that.setData({ checkImages: that.data.checkImages });
        })
      }
    })
  },
  removeimage: function (event) {
    var num = event.currentTarget.dataset.index;
    var currentImage = this.data.checkImages[num];
    currentImage.hasImage = false;
    currentImage.filePath = "";
    currentImage.imageUrl="";
    this.data.checkImages[num] = currentImage;
    this.setData({ checkImages: this.data.checkImages });
  },

  uploadImage: function (filePath, callback) {
    var that=this;
    wx.showLoading({
      title: '上传图片中'
    });
    this.setData({uploadflag:true});
    wx.uploadFile({
      url: app.globalData.serverPath + 'api/upload-pic-two',
      filePath: filePath,
      name: 'myfile',
      formData: {
        'lat': app.globalData.latitude,
        'lng': app.globalData.longitude
      },
      success: function (res) {
        var data = res.data
        var urldata = JSON.parse(res.data);
        if (urldata && urldata.url != "null") {
          if (callback) {
            callback(urldata);
          }
        } else {
          wx.showModal({
            title: '错误提示',
            content: '照片上传失败，请重试',
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.hideLoading();
        that.setData({ uploadflag: false });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initImages(6);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  submit:function(e){
    if (this.data.uploadflag)return;
    var images = [];
    for (var i = 0; i < this.data.checkImages.length; i++) {
      if (this.data.checkImages[i].imageUrl.length > 0) {
        images.push(this.data.checkImages[i].imageUrl);
      }
    }


    if (this.data.index>0){
      if (e.detail.value.rectItem==""){
        wx.showModal({
          title: '错误提示',
          content: "请输入整改事项",
          showCancel: false
        });
        return;
      }
      if (images.length==0) {
        wx.showModal({
          title: '错误提示',
          content: "请上传整改事项照片",
          showCancel: false
        });
        return;
      }
    }
    
    var pages = getCurrentPages();
    pages[1].setData({
      rectTotal:this.data.index,
      rectItem: e.detail.value.rectItem,
      zhenggaiImages:images,
    });

    console.log("zhenggai submit", pages, pages[0]);
    wx.navigateBack();
  }
})